<template>
	<view style="padding: 40rpx;">
		<u--form :model="form" ref="form">
			<u-form-item v-if="iscompany" label="公司名称" prop="name">

				<view class="" style="display: flex;">
					<!-- <input  class="input" v-model="form.companyId" /> -->
					<view class="">
						{{companyName}}
					</view>
				</view>

			</u-form-item>


			<u-form-item label="姓名:" prop="name">
				<input class="input" v-model="form.userName" />

			</u-form-item>

			<u-form-item label="手机号" prop="name">
				<input class="input" v-model="form.phone" />

			</u-form-item>

			<u-form-item label="性别:" prop="name">
				<u-radio-group v-model="form.sex">
					<u-radio :customStyle="{margin: '18px'}" v-for="(item, index) in actions" :key="index" :label="item.name" :name="item.value">
					</u-radio>
				</u-radio-group>
			</u-form-item>


			<u-form-item label="工号:" prop="name">
				<input class="input" v-model="form.userNumber" />

			</u-form-item>
		</u--form>
		<view class="bt">
			<u-button type="primary" @click="submit">保存</u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				companyName: "",
				iscompany: true,
				showSex: false,
				actions: [{
						name: '男',
						value: 1
					},
					{
						name: '女',
						value: 2
					},
				],
				form: {
					id: "",
					userNumber: "",
					userName: "",
					phone: "",
					sex: 0,
					companyId: "",
					userId: ""
				}
			}
		},
		methods: {
			 getUserInfo(){
				 this.form.userId=this.$store.state.userId 
			 },
			scanCode() {
				var that = this
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ["qrCode"],
					success: (res) => {
						console.log(res);
						if (res.result) {
							const val = res.result;
							that.form.companyId = res.result
						} else {

							console.log('请重新扫描');
							that.$refs.uToast.show({
								type: 'error',
								icon: false,
								title: '提示',
								message: "请重新扫描",
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
							})
							return false;
						}
					},
					fail: (res) => {
						that.$refs.uToast.show({
							type: 'error',
							icon: false,
							title: '提示',
							message: "未识别到二维码",
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
						})
						console.log('未识别到二维码');
					}
				})
			},

			submit() {
				var that = this
				this.api.post("/employee/register/", this.form).then(res => {
					console.log(res)
					if (res.code != 200) {
						this.$refs.uToast.show({
							type: 'error',
							icon: false,
							title: '提示',
							message: res.mes,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
						})
					} else {
						that.$store.state.employeeId = res.data.id
						that.$refs.uToast.show({
							type: 'default',
							title: '提示',
							message: res.mes,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/default.png'
						})
						uni.showLoading({
							title: '加载中'
						});

						that.$store.state.userName = res.data.userName
						that.$store.state.sex = res.data.sex
						that.$store.state.userNumber = res.data.userNumber
						that.$store.state.id = res.data.employeeId
						that.$store.state.userId = res.data.userId
						that.$store.state.myCompanyId = res.data.companyId
						that.$store.state.sex = res.data.sex
						that.$store.state.phone = res.data.phone


						setTimeout(function() {
							uni.hideLoading();
							that.$store.commit("getUserInfo")
							uni.navigateBack();
						}, 1500);

					}

				})
			},
		},
		onLoad(options) {
			// if( this.$store.state.myCompanyId!=""){
			// 	this.iscompany=false
			// }
			console.log(options)
			var that=this
			var getId=""
			if(Object.keys(options).length==0){
				this.form.userName = this.$store.state.userName
				this.form.sex = this.$store.state.sex
				this.form.userNumber = this.$store.state.userNumber
				this.form.id = this.$store.state.employeeId
				this.form.userId = this.$store.state.userId
				this.form.companyId = this.$store.state.myCompanyId
				this.form.sex = this.$store.state.sex
				this.form.phone = this.$store.state.phone
				getId=this.form.companyId
				console.log("非页面传参")
			}else{
				getId=options.id
			}
			this.form.companyId=getId
			this.api.get("/company/findInfo/"+getId, {}).then(res => {
				that.companyName=res.data.name
				that.$store.state.myCompanyName =  res.data.name
				that.$store.state.ownCompanyUserId = res.data.ownerUserId
			})
           
         
		
		},
		onShow() {
			this.getUserInfo()
		}
	}
</script>

<style>
	.bt {
		width: 95%;
		margin: 20rpx auto;
	}

	.input {
		border-bottom: 1rpx #BBBCC6 solid;
	}
</style>
